package cn.edu.bzu.ie.sf.mealsmisweb.mapper;

import cn.edu.bzu.ie.sf.mealsmisweb.entity.Userinfo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

/**
 * @version v1.0.0
 * @Package : cn.edu.bzu.ie.sf.mealsmisweb.mapper
 * @Description : 用户数据库操作类
 * @Create on : 2021/8/7 20:00
 * @author: Feng Song 宋锋
 **/
@Repository
public interface UserinfoMapper extends Mapper<Userinfo> {
//    @Results(id="userResultMap", value={
//            @Result(property = "id", column = "id", id = true),
//            @Result(property = "uname", column = "uname", jdbcType = JdbcType.VARCHAR),
//            @Result(property = "upass", column = "upass", jdbcType = JdbcType.VARCHAR),
//            @Result(property = "salt", column = "salt", jdbcType = JdbcType.VARCHAR),
//            @Result(property = "depid", column = "depid", jdbcType = JdbcType.INTEGER),
//            @Result(property = "depname", column = "depname", jdbcType = JdbcType.VARCHAR),
//            @Result(property = "dep_parent_name", column = "dep_parent_name" , jdbcType = JdbcType.VARCHAR),
//            @Result(property = "buse", column = "buse", jdbcType = JdbcType.BIT),
//            @Result(property = "creator", column = "creator", jdbcType = JdbcType.INTEGER),
//            @Result(property = "creator_name", column = "creater_name", jdbcType = JdbcType.VARCHAR),
//            @Result(property = "create_dt", column = "create_dt", jdbcType = JdbcType.TIMESTAMP)
//    })

    /**
     * 从视图中查询所有的用户数据信息
     * @return List<Userinfo> 用户列表
     */
    @Select("select * from vuserinfo ")
    List<Userinfo> getAllUserList();

    /**
     * 从视图中查看某个部门下的所有用户信息
     * @param depid 部门信息
     * @return List<Userinfo> 大队及其下面中队的用户列表
     */
    @Select("select * from vuserinfo where depid=#{depid} or dep_pid=#{depid}")
    List<Userinfo> getAllUserListByDepid(int depid);

    /**
     * 查询某个用户信息的方法
     * @param id 用户 的id
     * @return Userinfo实例
     */
    @Select("select * from vuserinfo where id=#{id}")
    Userinfo getUserinfoById(int id);

    /**
     * 通过用户名和密码查询用户信息
     * @param uname 用户名
     * @param upass 密码
     * @return Userinfo 实例
     */
    @Select("select * from vuserinfo where uname=#{uname} and upass=#{upass}")
    Userinfo getUserinfoByUnameAndUpass(String uname, String upass);

    /**
     * 通过用户名、电话、邮箱查询用户信息
     * @param uname 用户名
     * @param phone 密保手机号
     * @param email 密保邮箱
     * @return Userinfo 实例
     */
    @Select("select * from userinfo where uname=#{uname} and phone=#{phone} and email=#{email}")
    Userinfo getUserinfoByUnamePhoneEmail(String uname, String phone, String email);

    /**
     * 检查用户名的数量，用于检查用户名是否已经被使用了
     * @param uname 用户名
     * @return int 数量，0未被使用，>0已经使用了
     */
    @Select("select count(1) as sl from userinfo where uname=#{uname}")
    int chkUnameExist(String uname);

    /**
     * 添加用户信息
     * @param item 要添加的用户信息
     * @return int 影响的行数
     */
    @Insert("insert into userinfo(uname, upass, salt, depid, phone, email, buse, creator, create_dt) " +
            " values(#{uname}, #{upass}, #{salt}, #{depid}, #{phone}, #{email}, 1, #{creator}, #{create_dt})")
    int addUserinfo(Userinfo item);

    /**
     * 修改用户信息
     * @param item 要修改的用户信息
     * @return int 影响的行数
     */
    @Update("update userinfo set uname=#{uname}, upass=#{upass}, phone=#{phone}, email=#{email}, buse=#{buse} where id=#{id}")
    int editUserinfo(Userinfo item);

    /**
     * 删除用户信息
     * @param id 要删除用户的id
     * @return int 影响的行数
     */
    @Delete("delete from userinfo where id=#{id}")
    int delUserinfo(int id);
}
